package com.ruoyi.kenuo.service.impl;

import java.util.List;
import java.util.Optional;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kenuo.domain.KernelMyPatient;
import com.ruoyi.kenuo.mapper.KernelMyPatientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kenuo.mapper.KernelApplyMapper;
import com.ruoyi.kenuo.domain.KernelApply;
import com.ruoyi.kenuo.service.IKernelApplyService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author polo tang
 * @date 2023-09-05
 */
@Service
public class KernelApplyServiceImpl implements IKernelApplyService
{
    @Autowired
    private KernelApplyMapper kernelApplyMapper;
    @Autowired
    private KernelMyPatientMapper myPatientMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public KernelApply selectKernelApplyById(Long id)
    {
        return kernelApplyMapper.selectKernelApplyById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param kernelApply 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<KernelApply> selectKernelApplyList(KernelApply kernelApply)
    {
        return kernelApplyMapper.selectKernelApplyList(kernelApply);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param kernelApply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertKernelApply(KernelApply kernelApply)
    {
        kernelApply.setApplyTime(kernelApply.getCreateTime());
        kernelApply.setIsAgree(0);
        return kernelApplyMapper.insertKernelApply(kernelApply);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param kernelApply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateKernelApply(KernelApply kernelApply)
    {
        return kernelApplyMapper.updateKernelApply(kernelApply);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteKernelApplyByIds(Long[] ids)
    {
        return kernelApplyMapper.deleteKernelApplyByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteKernelApplyById(Long id)
    {
        return kernelApplyMapper.deleteKernelApplyById(id);
    }

    @Override
    public AjaxResult agreeAddPatient(KernelApply kernelApply) {
        Integer tge = kernelApply.getIsAgree();
        //拒绝只需要修改状态即可
        if(tge==2){
            KernelApply ky = new KernelApply();
            ky.setId(kernelApply.getId());
            ky.setIsAgree(2);
            ky.setRemarkThree("拒绝时间："+DateUtils.getNowDate().toString());
            kernelApplyMapper.updateKernelApply(ky);
            //拒绝后，假如要有医患关系，要删除掉
            KernelMyPatient kpate = new KernelMyPatient();
            kpate.setDoctorUname(kernelApply.getFromName());
            kpate.setClientUname(kernelApply.getToName());
            List<KernelMyPatient> plist = myPatientMapper.selectKernelCurPatientList(kpate);
            if(plist.size()>0){
                plist.stream().forEach(x->{
                    myPatientMapper.deleteKernelMyPatientById(x.getId());
                });
            }
            return AjaxResult.success();
        }
        if(StringUtils.isEmpty(kernelApply.getRemarkOne())||StringUtils.isEmpty(kernelApply.getRemarkTwo())){
            return AjaxResult.error("DoctorId or patientId is null,please check it! ");
        }
        if(tge==1){
            KernelApply kay = new KernelApply();
            kay.setId(kernelApply.getId());
            kay.setIsAgree(1);
            kay.setRemarkThree("同意时间："+DateUtils.getNowDate().toString());
            kernelApplyMapper.updateKernelApply(kay);
            //添加医患关系
            KernelMyPatient mt = new KernelMyPatient();
            mt.setDoctorUname(kernelApply.getFromName());
            mt.setDockerKname(kernelApply.getFromNickName());
            mt.setClientUname(kernelApply.getToName());
            mt.setClientKname(kernelApply.getToNickName());
            mt.setCreateTime(DateUtils.getNowDate());
            mt.setRemarkOne(kernelApply.getRemarkOne());
            mt.setRemarkTwo(kernelApply.getRemarkTwo());
            myPatientMapper.insertKernelMyPatient(mt);
        }

        return  AjaxResult.success();
    }

    @Override
    public int deleteByName(KernelApply kay) {
        return kernelApplyMapper.deleteByName(kay);
    }

    @Override
    public List<KernelApply> getMyList(KernelApply kernelApply) {

        return kernelApplyMapper.getMyList(kernelApply);
    }


}
